package com.example.java_gobang.config;

import com.example.java_gobang.api.GameAPI;
import com.example.java_gobang.api.MatchAPI;
import com.example.java_gobang.api.TestAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket //让Spring框架认识到这是一个配置websocket的类
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestAPI test;
    @Autowired
    private MatchAPI matchAPI;

    @Autowired
    private GameAPI gameAPI;
    //注册handler中到框架中去
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //当客户端连接到test路径时，就会执行TestAPI中对应的一些方法
        registry.addHandler(test, "/test");
        registry.addHandler(matchAPI, "/findMatch").
                addInterceptors(new HttpSessionHandshakeInterceptor());
        //握手拦截器
        //该拦截器会在WebSocket握手阶段：
        //将HTTP Session中的属性复制到WebSocket Session中
        //提供beforeHandshake和afterHandshake两个拦截点
        //支持会话属性传递和连接验证逻辑
        registry.addHandler(gameAPI, "/game").
                addInterceptors(new HttpSessionHandshakeInterceptor());

    }
}
